import axios from "axios";
import baseUrl from "../baseUrl";
import md5 from "js-md5"
export default{
    namespaced: true,
    state:{
        token:"",
        user:"",
        message:"",
    },
    actions:{
        login(context, data){
            console.log(data);
            axios
              .get(`${baseUrl}user/login`, {
                params: {
                  username: data.username,
                  password: md5(data.password),
                },
              })
              .then((response) => {
                console.log(response.data);
                context.commit("LOGIN", response.data);
                return response.data;
              })
              .catch((error) => {
                console.log(error.message);
              });

        }
    },
    mutations: {
        LOGIN(state, data){
          if(data.state==200){
            state.token = data.data.token
            state.user= data.data.user.username 
            localStorage.setItem('token',JSON.stringify(data.data.token))
            localStorage.setItem('username',JSON.stringify(data.data.user.username))
            state.message=""
          }else{
            state.token=""
            state.user= ""
            state.message=data.message
          }
        }

    }

}